﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CPETwit.Data.Entity;
using CPETwit.Web.Service.Implementation;
using CPETwit.Web.Service.Interface;


namespace CPETwit.Web.Pages
{
    public partial class Profil_edit : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            this.isLoggedViaSession();
            this.fillFormFields();
        }

        /// <summary>
        /// Remplissage des champs de la page
        /// </summary>
        protected void fillFormFields()
        {
            /* Valeurs par défaut des champs */
            switch (((User)Session["login"]).sex)
            {
                case "M":
                    this.Masculin.Checked = true;
                    this.Feminin.Checked = false;
                    break;
                case "F":
                    this.Masculin.Checked = false;
                    this.Feminin.Checked = true;
                    break;
            }

            this.login.Text = ((User)Session["login"]).login;
            this.name.Text = ((User)Session["login"]).name;
            this.email.Text = ((User)Session["login"]).email;
            this.birthdate.Text = Convert.ToString(((User)Session["login"]).birthdate);
            this.description.Text = ((User)Session["login"]).description;
            this.password.Text = ((User)Session["login"]).password ;
        }

        /// <summary>
        /// Test si l'internaute est connecté ;
        /// Si ce n'est pas le cas, on le redirige vers la page d'accueil
        /// </summary>
        private void isLoggedViaSession()
        {
            bool isConnected = false;
            foreach (var item in Session.Keys)
            {
                if (String.Equals(item, "login"))
                {
                    isConnected = true;
                    /* Transmission du sexe de l'utilisateur */
                    String sexe = ((User)Session["login"]).sex;
                    Session.Add("sexe", sexe);
                    break;
                }
            }
            if (!isConnected)
            {
                Response.Redirect("connexion.aspx");
            }
        }

        /// <summary>
        /// Action suite à click sur "Sauvegarder les modifications"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void saveUser(object sender, EventArgs e)
        {
            User user = (User)Session["login"];

            /* Upload de l'image en base */
            if (fileuploadImage.HasFile)
            {
                int length = fileuploadImage.PostedFile.ContentLength;
                //create a byte array to store the binary image data
                byte[] imgbyte = new byte[length];
                HttpPostedFile img = fileuploadImage.PostedFile;
                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);
                
                user.avatar = imgbyte;
            }

            // Récupération des champs
            if (this.Masculin.Checked)
            {
                user.sex = "M";
            }
            else if (this.Feminin.Checked)
            {
                user.sex = "F";
            }
            user.email = Request.Form["email"];
            user.description = Request.Form["description"];
            user.name = Request.Form["name"];
            user.login = Request.Form["login"];
            user.password = Request.Form["password"];
            if (!String.IsNullOrEmpty(Request.Form["birthdate"]))
            {
                user.birthdate = DateTime.Parse(Request.Form["birthdate"]);
            }

            ITwitterService twitterService = new TwitterService();
            twitterService.UpdateUser(user);

            this.fillFormFields();
        }

        /// <summary>
        /// On force la propriété Checked des RadioButton
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadioButton_Checked(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            switch (rb.ID)
            {
                case "Feminin" :
                    this.Feminin.Checked = true;
                    this.Masculin.Checked = false;
                    break;
                case "Masculin" :
                    this.Feminin.Checked = false;
                    this.Masculin.Checked = true;
                    break;
            }
        }
    }
}